<template>
	<view class="container">
		<view class="con-box" style="margin-bottom: 50rpx;">
			<u--form labelWidth="60" labelPosition="rgith" :model="model1" :rules="rules" ref="form1">
				<u-form-item required borderBottom prop="userInfo.name1" label="封面" ref="name1">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u-upload accept="image" @delete="uploadRemove" :fileList="fileList" @afterRead="uploadSuccess" maxCount="1"
							style="margin-left: 20rpx;" width="80" height="80" imageMode="center" :previewFullImage="true"
							uploadText="上传图片">
						</u-upload>
					</view>
				</u-form-item>
				<u-form-item required label="标题" prop="userInfo.name2" borderBottom ref="name2">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u--input placeholder="请填写" v-model="model1.userInfo.name2" border="none"></u--input>
					</view>
				</u-form-item>
				<u-form-item required @click="$tools.toUrl('/pages/comment/parsePage')" label="正文" prop="userInfo.name5" borderBottom ref="name5">
					<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;">
						<u--input placeholder="请完善" disabled disabledColor="#fff" v-model="model1.userInfo.name5" border="none"></u--input>
					</view>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<view v-if="!newsCompany">
					<u-form-item required label="作者" prop="userInfo.name3" borderBottom ref="name3">
						<view class="u-m-t-20 u-flex-1"  style="flex: 1;margin-top: 20rpx;">
							<u--input  placeholder="请填写" v-model="model1.userInfo.name3" border="none"></u--input>
						</view>
					</u-form-item>
					<picker @change="changeType" :range="tabList" range-key="classTypeName">
						<u-form-item required label="类别" prop="userInfo.name4" borderBottom ref="name4">
							<view class="u-m-t-20 u-flex-1"  style="flex: 1;margin-top: 20rpx;">
								<u--input disabled disabledColor="#fff" style="text-align: right;"  placeholder="请选择" v-model="model1.userInfo.name4" border="none"></u--input>
							</view>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</picker>
					<u-form-item borderBottom  prop="userInfo.name6" label="视频" ref="name6">
						<view class="u-m-t-20 u-flex-1" style="flex: 1;margin-top: 20rpx;display: flex;">
							<view class="input-box" style="flex: 1;" @click="chooseVideo">
								<u--input disabled disabledColor="#fff" style="text-align: right;flex: 1;"  placeholder="请选择" v-model="model1.userInfo.name6" border="none"></u--input>
							</view>
							<u-icon v-if="model1.userInfo.name6" name="close-circle-fill" style="margin: 0 40rpx;" @click="model1.userInfo.name6 = ''"></u-icon>
						</view>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-form-item  borderBottom prop="userInfo.name7" label="音频" ref="name7">
						<view class="u-m-t-20 u-flex-1"  style="flex: 1;margin-top: 20rpx;display: flex;">
							<view class="input-box" style="flex: 1;" @click="chooseCord">
								<u--input @click="chooseCord" disabled disabledColor="#fff" style="text-align: right;"  placeholder="请选择" v-model="model1.userInfo.name7" border="none"></u--input>
							</view>
							<u-icon v-if="model1.userInfo.name7"  name="close-circle-fill" style="margin: 0 40rpx;" @click="model1.userInfo.name7 = ''"></u-icon>
						</view>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
								
				</view>
				
			</u--form>
		</view>
		<my-auth-modal></my-auth-modal>
		<button style="width: 702rpx;margin-bottom: 20rpx;" type="primary" class="u-m-y-20" @click="$u.throttle(submit, 1000)">{{isEdit?'修改':'发布'}}</button>
	</view>
</template>

<script>
	import {
		uploadFile
	} from '@/shopro/utils/upload.js'
	import { mapGetters } from 'vuex';
	import {
		uploadVideo
	} from '@/shopro/utils/upload.js'
	import {
		filePath
	} from '@/env.js'
	import store from '@/store/index.js'
	export default {
		data() {
			return {
				model1: {
					userInfo: {
						name1: '',
						name2: '',
						name3: '',
						name4: '',
						name5: '',
						name6: '',
						name7: ''
					},
				},
				fileList: [],
				tabList:[],
				rules: {
					'userInfo.name1': {
						type: 'string',
						required: true,
						message: '请上传图片',
						trigger: ['blur', 'change']
					},
					'userInfo.name2': {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},
					'userInfo.name3': {
						type: 'string',
						required: true,
						message: '请填写作者',
						trigger: ['blur', 'change']
					},
					'userInfo.name4': {
						type: 'string',
						required: true,
						message: '请选择类别',
						trigger: ['blur', 'change']
					},
					'userInfo.name5': {
						type: 'string',
						required: true,
						message: '请填写正文',
						trigger: ['blur', 'change']
					},
				},
				classTypeId:'',
				errorType: ['message'],
				isEdit:false,
				acItem:{},
				fileListVideo:[],
				newsCompany: '',
			};
		},
		computed: {
			...mapGetters(['editHtml']),
		},
		onLoad:function(option){
			this.newsCompany = this.$token.getToken('newsCompany')
			if(option.itemId){
				this.isEdit = true
				this.getDetailById(option.itemId)
			}
			this.queryClassType()
		},
		onShow: function() {
			// if(getToken('AUTH_TYPE'))
			// 	store.state.user.authType = getToken('AUTH_TYPE');
			this.model1.userInfo.name5 = this.editHtml && this.editHtml.text
		},
		beforeDestroy:function(){
			this.$store.commit('setEditHtml', {
				html: '',
				text: ''
			})
		},
		methods: {
			getDetailById(id){
				if(!id){
					return;
				}
				let obj = {}
				let apiName = ''
				if(this.$token.getToken('newsCompany')){
					obj['companyNo'] = this.$token.getToken('newsCompany')
					obj['newsId'] = id
					apiName = 'newsData.queryNewsById'
				}else{
					this.$token.getToken('version') == 1 ? obj['userNo'] = this.$token.getToken('useNum') : obj['companyNo'] =
						this.$token.getToken('useNum')
					this.newType == 2 ? obj['newsId'] = id : obj['id'] = id
					this.newType == 2 ? apiName = 'newsData.queryPortalNewsById2' : apiName = 'newsData.queryPortalNewsById'
				}
				this.$http(apiName, obj, '加载中').then(res=>{
					//console.log(res,898989)
					let data = {}
					if(this.$token.getToken('newsCompany')){
						data = res
					}else{
						data = res.data
					}
					if(data && data.title){
						this.acItem = data
						//console.log(this.acItem)
						let obj={}
						if(this.$token.getToken('newsCompany')){
							 obj ={
								name1:this.acItem.imgTitle || '',
								name2:this.acItem.title || '',
								name5:this.acItem.comContent || '',
							}
							this.fileList = [{url:this.$filePath+'182/'+this.acItem.imgTitle}]
						}else{
							obj={
								name1:this.acItem.images || '',
								name2:this.acItem.title || '',
								name3:this.acItem.author || '',
								name4:this.acItem.topTypeName || '',
								name5:this.acItem.introduce || '',
								name6:this.acItem.videos || '',
								name7:this.acItem.audios || ''
							}
							this.fileList = [{url:this.$filePath+'101/'+this.acItem.images}]
							this.classTypeId = this.acItem.topTypeId || this.acItem.classTypeId
						}
						this.$store.commit('setEditHtml', {
							html: obj.name5,
							text: obj.name5
						})
						this.model1.userInfo = obj
						//console.log(obj,89898)
					}
				})
			},
			submit() {
				this.$refs.form1.validate().then(valid => {
					if (valid) {
						let currData = this.model1.userInfo
						let params = {}
						let apiName = ''
						if(this.$token.getToken('newType') == 1){
							params = {
								input_image:currData.name1,
								title: currData.name2,
								authors:currData.name3,
								classTypeId:this.classTypeId,
								classTypeName:currData.name4,
								introduce:this.editHtml.html,
								input_videos:currData.name6 || '',
								input_audios:currData.name7 || ''
							}
							apiName = 'common.insertData'
							this.$token.getToken('version') == 1?params['craftsmanId'] = this.$token.getToken('useNum'):params['companyNo'] = this.$token.getToken('useNum')
						}else{
							apiName = 'common.insertDataComp'
							params = {
								comContent:this.editHtml.html,
								companyId:this.$token.getToken('newsCompany'),
								title:currData.name2,
								imgTitle:currData.name1,
								chainTypeId:10016,
							}
							if(this.acItem && this.acItem.newsId){
								params['newsId']=this.acItem.newsId
							}
						}
						if(this.isEdit){
							params['id'] = this.acItem.id
							params['auditState'] = 0
						}
						//console.log(params,999)
						this.$http(apiName,params,'提交中').then(res=>{
							//console.log(res,888)
							if(res.code){
								this.$u.toast('操作成功')
								uni.navigateBack({})
								this.$store.commit('setIsFresh', true)
							}else{
								this.$u.toast('提交出错')
							}
						})
					}
				})
			},
			// 选择类别
			changeType(e){
				//console.log(e,8888)
				this.classTypeId = this.tabList[e.target.value].typeId
				this.model1.userInfo.name4 = this.tabList[e.target.value].classTypeName
			},
			
			queryClassType() {
				this.$http('newsData.queryClassType', {}, '加载中').then(res => {
					//console.log(res, 888)
					this.tabList = res.listData
				})
			},
			choosePlace(){
				uni.chooseLocation({
					success: res => {
						//console.log(res,888)
						this.model1.userInfo.name3 = res.address
					}
				})
			},
			// 上传图片成功
			uploadSuccess(e) {
				let acCode = ''
				if(this.$token.getToken('newsCompany')){
					acCode = 182
				}else{
					acCode = 101
				}
				uploadFile(e.file,acCode).then(res=>{
					if(res){
						this.model1.userInfo.name1 = res
						this.fileList = [{url: e.file.url}]
					}
				})
			},
			// 移除图片
			uploadRemove(index) {
				this.fileList = []
			},
			//选择视频
			chooseVideo(){
				uploadVideo(110).then(videoData=>{
					if(videoData){
						//console.log(videoData)
						this.model1.userInfo.name6 = videoData
						//console.log(this.model1)
					}
				})
			},
			chooseCord(){
				uni.chooseFile({
					count:1,
					sourceType:['camera', 'album'],
					sizeType: ['compressed'],
					name: 'file',
					success: (res) => {
						uploadFile({
							size: res.tempFiles[0].size,
							url: res.tempFilePaths[0]
						},110,5,102).then(audio=>{
							//console.log(audio)
							if(audio){
								this.model1.userInfo.name7 = audio
							}
						})
					}
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	.container{
		.con-box{
			background-color: #FFFFFF;
			padding: 0rpx 30rpx 0rpx 30rpx;
		}
		background-color: #F8F8F8;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-height: calc(100vh - var(--window-top));
	}
</style>
